package com.fly;

import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableScheduling;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@EnableCaching
@EnableScheduling
@SpringBootApplication
public class SimpleApplication
{
    public static void main(String[] args)
    {
        printParams(args);
        Stream.of(args).forEach(log::info);
        if (RandomUtils.nextBoolean())
        {
            SpringApplication.run(SimpleApplication.class, args);
            return;
        }
        log.info("########## Banner.Mode.OFF, LogStartupInfo: false");
        SpringApplication application = new SpringApplication(SimpleApplication.class);
        application.setBannerMode(Banner.Mode.OFF);
        application.setLogStartupInfo(false);
        application.run(args);
    }
    
    /**
     * 参数传递获取
     * 
     * @param args
     * @see [类、类#方法、类#成员]
     */
    private static void printParams(String[] args)
    {
        // docker-compose.yml -> environment -> USER
        String user = System.getenv("USER");
        
        // java -jar xxx.jar --user=jackson
        String userArg = Stream.of(args).filter(arg -> arg.contains("--user")).map(arg -> StringUtils.substringAfter(arg, "=")).collect(Collectors.joining());
        
        // java -jar -Duser=jackson xxx.jar
        String userProp = System.getProperty("user", "");
        
        // 按优先级获取
        String userName = StringUtils.defaultIfBlank(user, StringUtils.defaultIfBlank(userArg, userProp));
        
        log.info("user: {}", user);
        log.info("userArg: {}", userArg);
        log.info("userProp: {}", userProp);
        log.info("userName: {}", userName);
    }
}
